//
//  JiaoYiDetailViewController.m
//  DBAChain
//
//  Created by Evan on 2018/9/9.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "JiaoYiDetailViewController.h"
#import "taskJiaoYiService.h"
#import "HomePassWordView.h"
#import <Masonry.h>

@interface JiaoYiDetailViewController ()
@property (nonatomic,strong)    HomePassWordView*   viewPassword;
@property (weak, nonatomic) IBOutlet UIImageView *imgHeader;

@property (weak, nonatomic) IBOutlet UILabel *labelName;
@property (weak, nonatomic) IBOutlet UILabel *labelCJ;
@property (weak, nonatomic) IBOutlet UILabel *labelCJlv;

@property (weak, nonatomic) IBOutlet UILabel *labelNumber;

@property (weak, nonatomic) IBOutlet UILabel *labelPrice;

@property (weak, nonatomic) IBOutlet UILabel *labelXE;

@property (weak, nonatomic) IBOutlet UILabel *labelCNY;

@property (weak, nonatomic) IBOutlet UITextField *txtNumberGTE;

@property (weak, nonatomic) IBOutlet UITextField *txtNumberCNY;

@property (weak, nonatomic) IBOutlet UIButton *btnSubmit;

@property (weak, nonatomic) IBOutlet UILabel *labelFromTitle;
@property (weak, nonatomic) IBOutlet UIButton *btnAll;

@property (weak, nonatomic) IBOutlet UIView *viewCenter;



@property (nonatomic,strong) dataOrderBuySaleDetailInfo *modelInfo;

@end

@implementation JiaoYiDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"求购详情";
    
    [self.txtNumberCNY setEnabled:NO];
    
    self.modelInfo = [dataOrderBuySaleDetailInfo new];
//    [self.imgHeader sd_setImageWithURL:[NSURL URLWithString:@""]];
    self.labelName.text = self.modelDetail.userName;
    self.labelCJ.text = self.modelDetail.sucessCout;
    self.labelXE.text = [NSString stringWithFormat:@"限额:%@CNY",self.modelDetail.limitAmount];
    self.labelCNY.text = [NSString stringWithFormat:@"%@ CNY",self.modelDetail.orderAmount];
    self.labelCJlv.text = self.modelDetail.successRate;
    self.labelPrice.text = [NSString stringWithFormat:@"单价: %@CNY",self.modelDetail.price];
    self.labelNumber.text = [NSString stringWithFormat:@"数量: %ld GTE",(long)self.modelDetail.number];
    
    if (self.isGouMai) {
        self.viewCenter.hidden = NO;
         self.title = @"求购详情";
        [self.btnAll setTitle:@"全部卖出" forState:UIControlStateNormal];
        self.labelFromTitle.text = @"卖出GTE";
        [self.btnSubmit setTitle:@"立即卖出" forState:UIControlStateNormal];
    }else{
        self.viewCenter.hidden = YES;
        self.title = @"出售详情";
        [self.btnAll setTitle:@"全部买入" forState:UIControlStateNormal];
        self.labelFromTitle.text = @"买入GTE";
        [self.btnSubmit setTitle:@"立即买入" forState:UIControlStateNormal];
        
    }
    
    
    [self loadData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setBlackStatus:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)loadData{
    
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:self.modelDetail.orderNo forKey:@"orderNo"];
    [dict setValue:@(self.isGouMai ? 0 : 1) forKey:@"orderType"];
    [DKProgressHUD showLoading];
    [taskJiaoYiService getOrderBuyAndSaleDetail:dict success:^(dataOrderBuyAndSaleDetail *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            self.modelInfo = response.orderBuySaleDetail;
            self.txtNumberGTE.placeholder = @"请输入需要购买的GTE数量";//[NSString stringWithFormat:@"最大可卖%@",self.modelInfo.maxGteCoin];
            self.txtNumberCNY.placeholder = @"您输入的GTE数量的CNY价值";//[NSString stringWithFormat:@"最大可卖%@",self.modelInfo.maxRmbAmount];
            return;
        }
        [DKProgressHUD showErrorWithStatus:response.statusDesc];
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

- (IBAction)btnAllClick:(id)sender {
    self.txtNumberCNY.text = self.modelInfo.maxRmbAmount;
    self.txtNumberGTE.text = self.modelInfo.maxGteCoin;
}

-(HomePassWordView*)viewPassword{
    if (!_viewPassword ){
        _viewPassword = [[[NSBundle mainBundle] loadNibNamed:@"HomePassWordView" owner:nil options:nil] objectAtIndex:0];
        [_viewPassword initUI];
        [self.view addSubview:_viewPassword];
        _viewPassword.hidden = YES;
        [_viewPassword mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.and.right.mas_equalTo(self.view);
            make.top.offset(60);
            make.bottom.mas_equalTo(self.view).offset(0);
        }];
    }
    return _viewPassword;
}

- (IBAction)btnSubmitClick:(id)sender {
    
    ///进入输入密码的模块
    self.viewPassword.hidden = NO;
    __block JiaoYiDetailViewController *  blockSelf = self;
    self.viewPassword.blockSubmit = ^(NSString *pwd) {
        
        if(blockSelf.isGouMai){
            //求购页进来，需要进行卖出
            [blockSelf submitMC:pwd];
        }else{
            //卖出页进来，需要进行买入
            [blockSelf submitMR:pwd];
        }
    };
}

///进行买入流程
- (void)submitMR:(NSString *)pwd{
    
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:[UserCenter shareCenter].user.userNo forKey:@"userNo"];
    [dict setValue:self.modelDetail.orderNo forKey:@"orderNo"];
    [dict setValue:pwd forKey:@"payPassword"];
    
    [DKProgressHUD showLoading];
    [taskJiaoYiService getUserBuyTrade:dict success:^(dataTransSaleBuyGTE *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            [DKProgressHUD showSuccessWithStatus:@"交易成功"];
            [self.navigationController popViewControllerAnimated:YES];
            return;
        }
        [DKProgressHUD showErrorWithStatus:response.statusDesc];
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
    
}

///进行卖出流程
- (void)submitMC:(NSString *)pwd{
    CGFloat sNumber = [self.txtNumberGTE.text floatValue];
    CGFloat sPrice = [self.modelDetail.price floatValue];
    
    if (sNumber < 100 || sNumber > 2000 ) {
        [DKProgressHUD showErrorWithStatus:@"请输入100-2000个范围的GTE"];
        return;
    }
    
    NSString *strP = [NSString stringWithFormat:@"%.2f",sPrice];
    NSString *strPN = [NSString stringWithFormat:@"%.2f",sPrice*sNumber];
    
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setValue:[UserCenter shareCenter].user.userNo forKey:@"userNo"];
    [dict setValue:strP forKey:@"price"];
    [dict setValue:strPN forKey:@"amount"];
    [dict setValue:@(sNumber) forKey:@"number"];
    [dict setValue:@(1) forKey:@"orderType"];
    [dict setValue:self.modelDetail.limitAmount forKey:@"limitAmount"];
    [dict setValue:pwd forKey:@"payPassword"];
    
    [DKProgressHUD showLoading];
    [taskJiaoYiService getTransSaleBuyGTE:dict success:^(dataTransSaleBuyGTE *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            [DKProgressHUD showSuccessWithStatus:@"交易成功"];
            [self.navigationController popViewControllerAnimated:YES];
            return;
        }
        [DKProgressHUD showErrorWithStatus:response.statusDesc];
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}



#pragma mark - textChanage

- (IBAction)txtGTEChange:(UITextField*)sender {
    CGFloat floatCNY = [self.modelDetail.price floatValue] * [sender.text floatValue];
    if (floatCNY > [self.modelDetail.orderAmount floatValue]) {
        [DKProgressHUD showErrorWithStatus:@"超过可买入总数"];
        return ;
    }
    self.txtNumberCNY.text = [NSString stringWithFormat:@"%.2f",floatCNY];
}




@end
